<?php

/**
 *
 * @package    urCronPlugin
 * @subpackage urCronServiceDiggHistoryPlugin
 * @author     Marc Weistroff <marc.weistroff@gmail.com>
 * @version    $Id: urCronServiceDiggHistory.class.php 5 2009-06-09 09:15:55Z flambeur $
 */
class urCronServiceDiggHistory extends urCronServiceBase
{
  static function getServiceName()
  {
    return array('Digg History', 'urCronServiceDiggHistory');
  }

  protected function doCronJobRss()
  {
    $this->feed->registerXPathNamespace('media', 'http://search.yahoo.com/mrss/');
    foreach($this->feed->channel->item as $item)
    {
      if(Doctrine::getTable('urCronItem')->findOneByOriginalId((string)$item->guid))
        return;

      $this->object = new urCronItem();
      $publishedDate = date('Y-m-d H:i:s', strtotime((string)$item->published)); // or $item->pubDate() ???

      $data = array('original_id'                     => (string)$item->guid,
                    'original_content'                => (string)$item->description,
                    'original_title'                  => (string)$item->title,
                    'ur_cron_feed_id'                 => $this->getFeedId(),
                    'original_published_date'         => $publishedDate,
                    'original_link'                   => (string)$item->link,
                    'original_author'                 => (string)$item->author,
                    'is_published'                    => sfConfig::get('app_urcron_autopublish', true)
                   );


      $medias = $item->xpath('media:group/media:content');
      if(is_array($medias))
      {
        $i = 0;
        foreach($medias as $media)
        {
          $attr = array();
          foreach($media->attributes() as $k => $v)
          {
            $attr[$k] = (string)$v;
            $this->object->addData('media'.$i, serialize($attr));
          }
          $i++;
        }
      }
      $this->object->synchronizeWithArray($data);
      $this->cleanItem();
      $this->object->save();
    }
  }

  protected function cleanItem()
  {
    $this->object->setTitle($this->object->getOriginalTitle());
    $this->object->setContent($this->object->getOriginalContent());

    $dispatcher = sfContext::getInstance()->getEventDispatcher();
    $dispatcher->notify( new sfEvent($this, 'urcron.post_clean.digg_history'));
  }
}
?>
